package servlet;

import dao.GoodsDao;
import dao.TypeDao;
import entity.Goods;
import entity.Type;
import entity.User;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

@WebServlet("/goodsList")
public class GoodsPageDataServlet extends HttpServlet {
	
	@Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) 
            throws ServletException, IOException {
        doGet(request, response);
    }
	@Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) 
            throws ServletException, IOException {
		final int SIZE = 8;
		Integer typeId = null;
		try {
			typeId = Integer.parseInt(request.getParameter("selectType"));
		}catch (Exception e){
		}
		System.out.println("------------goodsList-------------");
		String goodsId = request.getParameter("selectGoodsID");
		String goodsName = request.getParameter("selectGoodsName");
		String supplier = request.getParameter("selectSupplier");
		StringBuilder condition = new StringBuilder();
		StringBuilder urlParams =new StringBuilder();
		ArrayList<Object> parmas = new ArrayList<>();
		condition.append("1=1");
		if (typeId != null){
			condition.append(" and type_id=?");
			parmas.add(typeId);
			urlParams.append("&selectType=" + typeId);
		}
		if(goodsId != null && !goodsId.isEmpty()){
			condition.append(" and goods_id like ?");
			parmas.add("%" + goodsId + "%");
			urlParams.append("&selectGoodsID=" + URLEncoder.encode(goodsId,"utf-8"));
		}
		if (goodsName != null && !goodsName.isEmpty()){
			condition.append(" and goods_name like ?");
			parmas.add("%" + goodsName + "%");
			urlParams.append("&selectGoodsName=" + URLEncoder.encode(goodsName,"utf-8"));
		}
		if (supplier != null && !supplier.isEmpty()){
			condition.append(" and supplier like ?");
			parmas.add("%" + supplier + "%");
			urlParams.append("&selectSupplier=" + URLEncoder.encode(supplier,"utf-8"));
		}
		Integer page = null;
		try {
			page = Integer.parseInt(request.getParameter("page"));
		} catch (Exception e) {
			page = 1;
		}
		int start = (page - 1)*SIZE;
		GoodsDao dao = new GoodsDao();
		List<Goods> list = dao.queryGoodsPage(start, SIZE, condition.toString(), parmas);
		int total = dao.total_goods(condition.toString(),parmas);
		int pageNum = total / SIZE;
		if (total % SIZE != 0) {
			pageNum += 1;
		}
		StringBuilder sb = new StringBuilder();
		for (int i = 1; i <= pageNum; i++) {
			String str = "<a href=\"#url\">第" + i + "页</a>&nbsp;&nbsp;";
			str = str.replace("#url", request.getContextPath() + "/goodsList?page=" + i + urlParams.toString());
			System.out.println("/goodsList?page=" + i + urlParams.toString());
			if (page == i) {
				str = "第" + i + "页&nbsp;&nbsp;";
			}
			sb.append(str);
		}
		System.out.println(sb);
		TypeDao typeDao = new TypeDao();
		List<Type> types = typeDao.selectAllTypes();
		request.setAttribute("page", sb.toString());
		request.setAttribute("goods", list);
		request.setAttribute("types", types);
		request.getRequestDispatcher("./backend/goods_list.jsp").forward(request, response);
	}


}
